<?php

require 'facebook.php';
require 'config.inc.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => $fbconfig['app_id'],
  'secret' => $fbconfig['secret_key'],
  'cookie' => true,
));

$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
  } catch (FacebookApiException $e) {
    echo "Somethings wrong";
  }
}

$con = mysql_connect("localhost", "vatsav", "2T*vzcd");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db("vatsav", $con);
if (!$db_selected) {
	die ('Cant use db : ' . mysql_error());
}

$id = $_POST["uid"];

$query = "select count(*) from links where uid=$id union all select count(*) from groups where uid=$id union all select count(*) from members where friendid=$id";

$result = mysql_query($query);	
// Check result
if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "<br/>";
   echo $message;
}
$row = mysql_fetch_array($result);
$num_links = $row[0];
$row = mysql_fetch_array($result);
$num_groups = $row[0];
$row = mysql_fetch_array($result);
$num_groups += $row[0];



try{
	$fql    =   'SELECT uid FROM user WHERE is_app_user="true" and uid in (select uid2 from friend where uid1='.$id.')';
	$param  =   array(
		'method'    => 'fql.query',
		'query'     => $fql,
		'callback'  => ''
	);
	$result   =   $facebook->api($param);
}catch(Exception $o){
	echo($o);
}

$num_friends = sizeof($result);

mysql_close($con);
?>


<!-- Linkstar Level - not important now -->
<div class="header"> In Linkstar </div>
Number of Links: <?php echo $num_links; ?> <br/>
Number of Friends: <?php echo $num_friends; ?> <br/>
Number of Groups : <?php echo $num_groups; ?> <br/>
<br/>

<!-- share link to post it on facebook wall -->
<?php 

if($uid == $id)
{
?>
	<a onclick="javascript:stream()"> Share your thoughts about Linkstar to your friends </a>

<?php
}
?>